package com.lhkj.ct.platform.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lhkj.ct.base.enums.ReturnStatus;
import com.lhkj.ct.base.exception.BusinessException;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.MessageUtils;
import com.lhkj.ct.base.utils.ShiroUtil;
import com.lhkj.ct.framework.auth.service.AuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("api/my/web/auth")
@Api(tags = {"01.授权接口"})
@ApiSupport(order = 1)
public class MyAuthController {

    @Resource
    private AuthService authService;

    @ResponseBody
    @ApiOperation(value = "登录")
    @ApiOperationSupport(order = 1)
    @PostMapping("login")
    public ActionResult<?> login(@RequestBody Map<String, String> params){
        if (SecurityUtils.getSubject().isAuthenticated()) {
            return ActionResult.fail("您已登录，请勿重复登录！");
        }
        if (!params.containsKey("loginData")) {
            throw new BusinessException(ReturnStatus.SC_BAD_REQUEST, "登录参数为空！");
        }
        String token = authService.webPlatformLogin(params.get("loginData"));
        return ActionResult.success(MessageUtils.message("user.login.success"), token);
    }

    @ResponseBody
    @ApiOperation(value = "注销/退出登录")
    @ApiOperationSupport(order = 2)
    @PostMapping("logout")
    public ActionResult<Void> logout() {
        Subject subject = ShiroUtil.getSubject();
        subject.logout();
        return ActionResult.success(MessageUtils.message("user.logout.success"));
    }

    @ResponseBody
    @ApiOperation(value = "获取验证码")
    @ApiOperationSupport(order = 3)
    @GetMapping("captcha")
    public ActionResult<Map<String, Object>> captcha(HttpServletRequest request) {
        return ActionResult.success(ReturnStatus.SUCCESS.getDescription(), authService.getCaptcha(request));
    }
}
